package com.nord.Menus;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nord.R;
import com.nord.EventList.EventList;
import com.nord.Login.Login;
import com.nord.NewsList.NewsList;
import com.nord.PhoneBook.PhoneBook;
import com.nord.Services.NotificationBroadcastReciever;
import com.nord.Services.SQLiteAdapter;
import com.nord.Stalker.Stalker;

public class Menus extends Activity {

	Button news, event, stalker, phoneBook, menuBanner;
	Typeface tf;
	TextView menuUsername;
	String supername, username;
	public SharedPreferences prefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menus);
		news = (Button) findViewById(R.id.menunews);
		event = (Button) findViewById(R.id.menuevent);
		stalker = (Button) findViewById(R.id.menustalker);
		phoneBook = (Button) findViewById(R.id.menuphonebook);
		stalker.setClickable(false);
		
		menuUsername = (TextView) findViewById(R.id.menusername);
		menuBanner = (Button) findViewById(R.id.menuBanner);
		tf = Typeface.createFromAsset(getAssets(), "DaysOne-Regular.ttf");
		menuBanner.setTypeface(tf);
		
		//virkjum alarm til að virkja notificaitons þegar við á.
		setRepeater();

		username = getIntent().getExtras().getString("USERNAME");
		/* Gender check */
		if (username.contains(("dóttir"))) {
			menuUsername.setText("Velkomin\n" + username);
		} else {
			menuUsername.setText("Velkominn\n" + username);
		}

		menuBanner.setOnClickListener(new View.OnClickListener() {
			private int clickCount = 0;
			
			@Override
			public void onClick(View v) {
				if(clickCount != 9)
					clickCount++;
				else{
					Intent visnaNordIntent = new Intent(Menus.this, VisnaNord.class);
					visnaNordIntent.putExtra("USERNAME", username);
					startActivity(visnaNordIntent);
					clickCount = 0;
				}
			}
		});

		/* Move to NewsList activity */
		news.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent newsIntent = new Intent(Menus.this, NewsList.class);
				newsIntent.putExtra("USERNAME", username);
				startActivity(newsIntent);
			}
		});

		/* Move to EventList activity */
		event.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent eventIntent = new Intent(Menus.this, EventList.class);
				eventIntent.putExtra("USERNAME", username);
				startActivity(eventIntent);
			}
		});

		/* Move to Stalker activity */
		stalker.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// Vid faerum okkur i EltiHrellir
				Intent stalkerIntent = new Intent(Menus.this, Stalker.class);
				stalkerIntent.putExtra("USERNAME", username);
				startActivity(stalkerIntent);
			}
		});

		/* Move to PhoneBook activity */
		phoneBook.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//
				Intent phoneBook = new Intent(Menus.this, PhoneBook.class);
				phoneBook.putExtra("USERNAME", username);
				startActivity(phoneBook);
			}
		});

		prefs = PreferenceManager.getDefaultSharedPreferences(Menus.this);

		if(prefs.getBoolean("newsNotification", true) == false && prefs.getBoolean("visoNotification", true) == false){
			Toast toast = Toast.makeText(Menus.this, "Það er slökkt á tilkynningum!", Toast.LENGTH_LONG);
			toast.show();
		}
	}

	@Override
	public void onBackPressed() {
		/* Exit Application */
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "Skrá mig út");
		menu.add(0, 2, 0, "Stillingar");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			invokeLogoutDialog();
			break;
		case 2:
			invokeSettings();
			break;
		}
		return true;
	}

	void invokeSettings() {
		Intent intent = new Intent(Menus.this, PrefsActivity.class);
		startActivity(intent);
	}

	void invokeLogoutDialog() {
		final AlertDialog dialog = new AlertDialog.Builder(Menus.this).create();
		dialog.setTitle("Úrskráning úr Appinu");
		dialog.setMessage("Ertu viss að þú vilt skrá þig úr appinu ?\n\nÚrskráning eyðir öllum upplýsingum sem hafa verið vistaðar");

		dialog.setButton2("Nyet", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		dialog.setButton("Já", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				new Logout().execute();
			}
		});

		dialog.show();
	}

	private class Logout extends AsyncTask<Void, Integer, Void> {
		ProgressDialog progressBar;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar = new ProgressDialog(Menus.this);
			progressBar.setCancelable(false);
			progressBar.setMessage("Er að skrá þig úr appinu ...");
			progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressBar.setProgress(0);
			progressBar.setMax(100);
			progressBar.show();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			SQLiteAdapter sqLiteadapter = new SQLiteAdapter(Menus.this);
			sqLiteadapter.open("W");
			sqLiteadapter.logout();
			sqLiteadapter.close();
			return null;
		}

		@Override
		protected void onPostExecute(Void v) {
			progressBar.dismiss();
			Intent intent = new Intent(Menus.this, Login.class);
			startActivity(intent);

		}
	}
	
	//virkjar alarmnager og pingar NotificationBroadcastReciever reglulega 
	private void setRepeater() {
		AlarmManager aM = (AlarmManager)getApplication().getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(Menus.this, Menus.class);
		intent.addFlags(Intent.FILL_IN_DATA);
		intent.putExtra("USERNAME", username);
		PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		
		aM.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 15, pi);
	}
}






